package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result<String> save(@RequestBody Category category){
        log.info("category:{}",category);
       categoryService.save(category);
       return Result.success("菜品添加成功");
    }

    @GetMapping("/page")
    public Result<Page> page(Integer page,Integer pageSize){
        Page<Category> categoryPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort).orderByAsc(Category::getName);
        categoryService.page(categoryPage,lqw);
        return Result.success(categoryPage);
    }

    @DeleteMapping
    public Result<String> delete(Long id){
        log.info("删除的id：{}",id);
        categoryService.remove(id);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result<String> update(@RequestBody Category category){
        log.info("修改分类信息:{}",category);
        categoryService.updateById(category);
        return Result.success("修改成功");
    }

//根据传入的Type不同，来获取菜品分类或套餐分类数据
    @GetMapping("/list")
    public Result<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return Result.success(list);
    }
}
